home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / YICN23.ZIP / UNITS / YAKSCROL.CPP < prev    next >
C/C++ Source or Header  |  1993-02-27  |  5KB  |  177 lines

  1. #include "yakscrol.h"
  2. #include "yakmouse.h"
  3. #include <string.h>
  4. #include <conio.h>
  5.  
  6. extern yakMouse mouse;
  7. //yakScroller functions follow------------------------------------------>
  8.  
  9. void yakScroller::draw(word offset)
  10. {
  11.   yakWindow::draw(offset);
  12.   drawText(offset);
  13. }
  14.  
  15. word yakScroller::interpretMouseClick(void)
  16. {
  17.   if (mouse.isInBox(x+2, y, x+width-2, y+2))
  18.     lineMove(up, myOffset);
  19.   if (mouse.isInBox(x+2, y+height-2, x+width-2, y+height-1))
  20.     lineMove(down, myOffset);
  21.   yakWindow::interpretMouseClick();
  22.   return 0;
  23. }
  24.  
  25. word yakScroller::interpretKeyStroke(char myChar)
  26. {
  27.   if (myChar == 0)
  28.   {
  29.     myChar = getch();
  30.     switch(myChar)
  31.     {
  32.       case 72 : lineMove(up, myOffset); break;
  33.       case 80 : lineMove(down, myOffset); break;
  34. //    case 75 : cursor.mapX--; break;
  35. //    case 77 : cursor.mapX++; break;
  36. //    case 71 : cursor.mapY--; cursor.mapX--; break;
  37.       case 73 : pageMove(up, myOffset); break;
  38. //    case 79 : cursor.mapY++; cursor.mapX--; break;
  39.       case 81 : pageMove(down, myOffset); break;
  40.     }
  41.   }
  42.   if (myChar == 27)
  43.     close();
  44.   return 0;
  45. }
  46. char * yakScroller::getLine(word lineNumber)
  47. {
  48.   byte exit = 0;
  49.   byte charsInALine = (width - 5) / CharWidth;
  50.   int counter;
  51.   char * thisLine = myText;
  52.   char * lineCounter; //position of line
  53.   word currentLineNumber = 0; //number of line
  54.   while((*thisLine != 0) && (currentLineNumber != lineNumber))
  55.   {
  56.     exit = 0;
  57.     for (counter = 0, lineCounter = thisLine; counter < charsInALine; ++counter)
  58.     {
  59.       if (*lineCounter == 0x0d)
  60.       {
  61.         thisLine = lineCounter + (((char)*(lineCounter + 1) == (char)0x0a) ? 2 : 1);
  62.         lineCounter = thisLine;
  63.         currentLineNumber++;
  64.         exit = 1;
  65.       }
  66.       if (!exit) ++lineCounter;
  67.     }
  68.     while (!exit)//lineCounter >= thisLine)
  69.     {
  70.       if (*lineCounter == ' ')
  71.       {
  72.     currentLineNumber++;
  73.     thisLine = lineCounter + 1;
  74.     exit = 1;
  75.       }
  76.       else if (*lineCounter == 0)
  77.     return lineCounter;
  78.       else if (lineCounter == thisLine)
  79.       {
  80.     lineCounter = thisLine = thisLine + charsInALine;
  81.     currentLineNumber++;
  82.     exit = 1;
  83.       }
  84.       else
  85.     lineCounter--;
  86.     }
  87.   }
  88.   return thisLine;
  89. }
  90.  
  91. int yakScroller::lineLength(word lineNumber)
  92. {
  93.   char * thisLine = getLine(lineNumber);
  94.   byte charsInALine = (width - 5) / CharWidth;
  95.   for (int counter = 0; counter <= charsInALine; ++counter)
  96.     if ((* (thisLine + counter) == 0) || (* (thisLine + counter) == 13))
  97.       return(counter);
  98.   for (counter = charsInALine; counter >= 0; --counter)
  99.     if (*(thisLine+counter) == ' ')
  100.       return counter;
  101.   return charsInALine;
  102. }
  103.  
  104. void yakScroller::drawLine(int lineNumber, int x, int y, word offset)
  105. {
  106.   int charsInALine = (width - 5) / CharWidth;
  107.   char *thisLine = new char[charsInALine];
  108.   int thisLineLength = lineLength(lineNumber);
  109.   strncpy(thisLine, getLine(lineNumber), thisLineLength);
  110.   for (int counter = thisLineLength; counter < charsInALine; ++counter)
  111.     thisLine[counter] = ' ';
  112.   thisLine[charsInALine] = 0;
  113.   x_bgprintf(x, y, offset, textColor, boxColor, "%s", thisLine);
  114.   delete thisLine;
  115. }
  116.  
  117. void yakScroller::drawText(int lineNumber, int ix, int iy, word offset)
  118. {
  119.   x = ix;
  120.   y = iy;
  121.   byte linesInABox = (height - CharHeight - 5) / CharHeight;
  122.   for(int lineCounter = 0; lineCounter < linesInABox; ++lineCounter)
  123.     drawLine(lineCounter + lineNumber, ix+3, iy+CharHeight+3+lineCounter*CharHeight, offset);
  124. }
  125.  
  126. void yakScroller::drawText(int lineNumber, word offset)
  127. {
  128.   drawText(lineNumber, x, y, offset);
  129. }
  130.  
  131. void yakScroller::drawText(word offset)
  132. {
  133.   drawText(position, x, y, offset);
  134. }
  135.  
  136. void yakScroller::lineMove(direction pageDirection, word offset)
  137. {
  138.   position += (pageDirection == down) ? 1 : -1;
  139.   drawText(offset);
  140. }
  141.  
  142. void yakScroller::pageMove(direction lineDirection, word offset)
  143. {
  144.   position += (lineDirection == down) ? ((height - 15) / CharHeight) : -((height - 5) / CharHeight);
  145.   if (position < 0) position = 0;
  146.   drawText(offset);
  147. }
  148.  
  149. void yakScroller::newText(byte * theNewText) //deletes old text!
  150. {
  151.   if (myText)
  152.     delete myText;
  153.   myText = theNewText;
  154.   position = 0;
  155. }
  156.  
  157. void yakScroller::activate(int x1, int y1, int x2, int y2, word offset, char * theNewText)
  158. {
  159.   width = x2-x1;
  160.   height = y2-y1;
  161.   newText(theNewText);
  162.   activate(x, y, offset);
  163. }
  164.  
  165. void yakScroller::activate(int ix, int iy, word ioffset)
  166. {
  167.   myOffset = ioffset;
  168.   x = ix; y = iy;
  169.   open();
  170.   char exit = 0;
  171.   while (exit != 27)
  172.   {
  173.     exit = getch();
  174.     interpretKeyStroke(exit);
  175.   }
  176. }
  177.